<template>
  <el-dialog :title="titleRation" append-to-body :close-on-click-modal="false" @close="closeDialog()" v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号" prop="moble">
            <el-input v-model="dataForm.moble" placeholder="手机号"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="停车场" prop="parkIds">
            <el-select v-model="dataForm.parkIds" multiple placeholder="停车场" style="width:100%">
              <el-option v-for="item in parkNames" :key="item.id" :label="item.parkName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/roadStop/parkingoperation'
import { fetchList } from '@/api/roadStop/regionroad'

export default {
  data () {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {

      },
      dataRule: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],

        moble: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],

        parkIds: [
          { required: true, message: '停车场不能为空', trigger: 'blur' }
        ],

      },
      parkNames: [],
      titleRation: null
    }
  },
  created () {
    this.getDataListTwo()
  },
  methods: {
    getDataListTwo () {
      fetchList({ isRoad: 1 }).then(response => {
        this.parkNames = response.data.data
      })
    },
    init (id) {
      if (id) {
        this.titleRation = '修改'
      } else {
        this.titleRation = '增加'
      }
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
            this.dataForm.parkIds = this.dataForm.parkIds.split(',')
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          this.dataForm.parkIds = this.dataForm.parkIds.join(',')
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
